package com.shujia.flink.state;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;

public class Demo01State {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> ds01 = env.socketTextStream("master", 8888);

        SingleOutputStreamOperator<Tuple2<String,Integer>> wordDS = ds01.flatMap((line, out) -> {
            for (String word : line.split(",")) {
                out.collect(Tuple2.of(word, 1));
            }
        }, Types.TUPLE(Types.STRING, Types.INT));

        KeyedStream<Tuple2<String, Integer>, String> keyedDS = wordDS.keyBy(t2 -> t2.f0, Types.STRING);


        // 基于分组之后的数据流同样可以调用process方法
        keyedDS
                .process(new KeyedProcessFunction<String, Tuple2<String, Integer>, String>() {
                    /**
                     * 为了将每个单词当前的计算结果保存起来
                     * 以便下一次使用
                     *
                     * 当前的结果——状态
                     *
                     * 状态：FLink实时任务某一时刻的计算结果以及消费的偏移量
                     *
                     * 由于HashMap是将数据存储在内存中的，所以状态不能够被持久化
                     * 如果程序发生了故障，那么无法恢复
                     * 所以需要一种可靠的存储系统，例如HDFS
                     * 将状态保存到HDFS的过程——CheckPoint
                     */
                    HashMap<String, Integer> wordCntMap;

                    // 当KeyedProcessFunction构建时只会执行一次
                    @Override
                    public void open(Configuration parameters) throws Exception {
                        wordCntMap = new HashMap<String, Integer>();
                    }

                    // 每一条数据会执行一次
                    @Override
                    public void processElement(Tuple2<String, Integer> value, KeyedProcessFunction<String, Tuple2<String, Integer>, String>.Context ctx, Collector<String> out) throws Exception {
                        // 通过process实现word count
                        // 判断word是不是第一次进入，通过HashMap查找word是否有count值
                        String word = value.f0;
                        int cnt = 1;
                        if (wordCntMap.containsKey(word)) {
                            int newCnt = wordCntMap.get(word) + 1;
                            wordCntMap.put(word, newCnt);
                            cnt = newCnt;
                        } else {
                            wordCntMap.put(word, 1);
                        }
                        out.collect(word + ":" + cnt);
                    }
                }).print();

        env.execute();
    }
}
